﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Text;
using System.Collections.Generic;
using Catalyst.Web.DynamicData;

namespace sistemaFinanceiroAriano.DynamicData.FilterTemplates
{
    public partial class ForeignKey_MultiSelect : Catalyst.Web.DynamicData.InFilterUserControlBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            PopulateListControl(ListBox1);
        }
        
        public override string ValueList
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach(ListItem li in ListBox1.Items)
                {
                    if (li.Selected)
                        sb.AppendFormat("{0},", li.Value);
                }
                return sb.ToString();
            }
            set
            {
                ListBox1.SelectedIndex = -1;
                string[] values = value.Split(',');
                foreach (string val in values)
                {
                    ListItem li = ListBox1.Items.FindByValue(val);
                    if (li != null)
                        li.Selected = true;
                }
            }
        }

        public override TypeCode ValueType
        {
            get { return ForeignKeyColumn.ParentTable.PrimaryKeyColumns[0].TypeCode; }
        }

        public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
        {
            yield return new InExpressionParameter() {  Name = string.Format("{0}.{1}", Column.Name, ForeignKeyColumn.ParentTable.PrimaryKeyColumns[0].Name),
                                                        ValueType = ValueType,
                                                        ValueList = ValueList };
        }
    }
}